﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BankService.CallbackInterface;
using BankService.ServiceInterface;

namespace BankClient {

    public partial class BankingForm : Form {

        private BankServiceProxy bankService;
        private BankServiceProxy.NewTransferHandler newTransferHandler;
        private LogInOutput logInOutput;
        private Form parent;

        public BankingForm(BankServiceProxy bankService, LogInOutput logInInfo, Form parent) {
            InitializeComponent();

            this.bankService = bankService;
            this.logInOutput = logInInfo;
            this.parent = parent;

            // Set up UI
            this.lblName.Text = this.logInOutput.Username;
            this.lblAddress.Text = this.logInOutput.Address;
            this.lblAccountNumber.Text = Convert.ToString(this.logInOutput.AccountNumber);
            this.lblBalance.Text = Convert.ToString(this.logInOutput.Balance);
            foreach (string m in logInInfo.Messages)
                this.lbEvents.Items.Add(m);

            // Events
            this.newTransferHandler = new BankServiceProxy.NewTransferHandler(this.bankService_NewTransfer);
            this.bankService.NewTransfer += this.newTransferHandler;
            this.FormClosing += new FormClosingEventHandler(this.BankingForm_FormClosing);
            this.btnLogOut.Click += new EventHandler(this.BankingFormBtn_Click);
            this.btnTransfer.Click += new EventHandler(this.BankingFormBtn_Click);
        }

        private void bankService_NewTransfer(double amount, string message) {
            this.Invoke((MethodInvoker)(delegate() {
                this.lblBalance.Text = Convert.ToString(Convert.ToDouble(this.lblBalance.Text) + amount);
                this.lbEvents.Items.Add(message);
            }));
        }

        private void BankingForm_FormClosing(object sender, FormClosingEventArgs e) {
            this.bankService.NewTransfer -= this.newTransferHandler;
            this.bankService.LogOut(this.logInOutput.SessionId);
            this.parent.Show();
        }

        private void BankingFormBtn_Click(object sender, EventArgs e) {
            Button b = sender as Button;
            if (b.Equals(this.btnLogOut)) {
                this.bankService.LogOut(this.logInOutput.SessionId);
                this.Close();
            } else if (b.Equals(this.btnTransfer)) {
                if (Convert.ToInt32(this.txtAccountNumber.Text) == this.logInOutput.AccountNumber)
                    MessageBox.Show("You cannot transfer money to yourself.");
                else {
                    bool successfulTransfer = this.bankService.Transfer(
                        this.logInOutput.SessionId,
                        Convert.ToInt32(this.txtAccountNumber.Text),
                        Convert.ToDouble(this.txtAmount.Text)
                        );
                    if (successfulTransfer) {
                        this.lbEvents.Items.Add(DateTime.UtcNow + " - You have sent " + this.txtAmount.Text + " to account number " + this.txtAccountNumber.Text + ".");
                        this.lblBalance.Text = Convert.ToString(Convert.ToDouble(this.lblBalance.Text) - Convert.ToDouble(this.txtAmount.Text));
                    } else
                        MessageBox.Show("An error occured and the transfer was not made.");
                }
            }
        }
    }
}
